﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using EShop.Common;
using System.Reflection;
using EShop.BLL;
using DateConvert;
using System.Threading;
using System.Drawing.Printing;
namespace EShop.Client
{
    public partial class frmMain : frmBase
    {
        #region 时间委托
        private void GetTime()
        {
            this.tssTime.Text = DateTime.Now.ToString("HH:mm:ss ddd");
            while (true)
            {
                Thread.Sleep(980);
                AsyncMethod.ControlsEndueValueEventHandler OnToolScriptStatuLabelEndueValue = new AsyncMethod.ControlsEndueValueEventHandler(OnToolStripStatusLabelEndueValueEvent);
                this.BeginInvoke(OnToolScriptStatuLabelEndueValue, new object(), new AsyncMethod.EndueValueEventArgs(tssTime, DateTime.Now.ToString("HH:mm:ss ddd"), OnToolScriptStatuLabelEndueValue));
            }
        }

        #endregion

        public frmMain()
        {
            InitializeComponent();
        }
        private void frmMain_Load(object sender, EventArgs e)
        {
            iMaxPageSize = oShopConfigInfo.PageSize;
            tssMsg.Text = "今天是" + DateTime.Now.ToString("yyyy年MM月dd日") + "(农历：" + new DateConvert.DateConvert().GetLunarCalendar(DateTime.Now) + ")  |  "
                + new DateConvert.DateConvert().GetConstellationName(DateTime.Now) + "  |  ";
            Thread threadGetTime = new Thread(new ThreadStart(GetTime));
            threadGetTime.IsBackground = true;
            threadGetTime.Start();
            lblEmployeeName.Text = string.Format("欢迎您：{0}",oShopConfigInfo.EmployeeFullName);
            if (!string.IsNullOrEmpty(oShopConfigInfo.LastLoginTime))
                lblLastLoginTime.Text = string.Format("上次登录时间：{0}", oShopConfigInfo.LastLoginTime);
            CheckConfigSetting();
        }

        private void CheckConfigSetting()
        {
            if (oShopConfigInfo.AutoCheckBrithday && CheckPermission(oShopConfigInfo.Permission, PermissionEnum.ToolManager)) OpenWindow(typeof(frmRemindBrithday).ToString());
            if (oShopConfigInfo.AutoCheckLowStock && CheckPermission(oShopConfigInfo.Permission, PermissionEnum.MerchandiseManager)) OpenWindow(typeof(frmMerchandiseStockLowAlertCheck).ToString());
            if (oShopConfigInfo.AutoCheckOverTime && CheckPermission(oShopConfigInfo.Permission, PermissionEnum.MerchandiseManager)) OpenWindow(typeof(frmMerchandiseOverTimeCheck).ToString());
            LayoutMdi(MdiLayout.TileHorizontal);

        }

        //关闭窗体事件
        private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            DialogResult dialogResult = MessageBox.Show("您真的退出商店管理系统吗？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dialogResult == DialogResult.Yes) Application.ExitThread();
            else e.Cancel = true;
        }

        //权限设置
        private void tlsMenu_PermissionManger_Click(object sender, EventArgs e)
        {
            if (CheckPermission(oShopConfigInfo.Permission, PermissionEnum.ToolManager))
            {
                frmPermissionManger permissionManger = new frmPermissionManger();
                permissionManger.Show();
            }
            else
            {
                MessageBox.Show("权限不足！");
                return;
            }
        }

        //商品类型管理
        private void tlsMenu_MerchandiseClassManager_Click(object sender, EventArgs e)
        {
            //frmMerchandiseClass oMerchandiseClass = new frmMerchandiseClass();
            //oMerchandiseClass.Owner = this;
            ////merchandiseClass.Parent = this;
            //oMerchandiseClass.ShowDialog();
            if (!CheckPermission(oShopConfigInfo.Permission, PermissionEnum.MerchandiseManager))
            {
                MessageBox.Show("权限不足！");
                return;
            }
            OpenWindow(typeof(frmMerchandiseClass).ToString());


        }

        //员工管理
        private void tsmiEmployeeManager_Click(object sender, EventArgs e)
        {
            //frmEmployeeManager oEmployeeManager = new frmEmployeeManager();
            //oEmployeeManager.Owner = this;
            //oEmployeeManager.ShowDialog();
            if (!CheckPermission(oShopConfigInfo.Permission, PermissionEnum.ToolManager))
            {
                MessageBox.Show("权限不足！");
                return;
            }
            OpenWindow(typeof(frmEmployeeManager).ToString());

        }

        //商品管理
        private void tlsMenu_MerchandiseBaseInfo_Click(object sender, EventArgs e)
        {
            //frmMerchandiseBasicManger obaseInfo=new frmMerchandiseBasicManger();
            //obaseInfo.Owner = this;
            //obaseInfo.ShowDialog();
            if (!CheckPermission(oShopConfigInfo.Permission, PermissionEnum.MerchandiseManager))
            {
                MessageBox.Show("权限不足！");
                return;
            }
            OpenWindow(typeof(frmMerchandiseBasicManger).ToString());

        }

        //添加库存
        private void 添加库存ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //frmStockIn oStockIn = new frmStockIn();
            //oStockIn.Owner = this;
            //oStockIn.ShowDialog();
            if (!CheckPermission(oShopConfigInfo.Permission, PermissionEnum.BaseManager))
            {
                MessageBox.Show("权限不足！");
                return;
            }
            OpenWindow(typeof(frmStockIn).ToString());

        }

        private void 我的信息ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenWindow(typeof(frmEmployeeInfoCurrentEmployee).ToString());
        }

        private void 修改密码ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenWindow(typeof(frmEmployeeChangePassword).ToString());
        }

        private void 开始收银ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (!CheckPermission(oShopConfigInfo.Permission, PermissionEnum.CashManager))
            {
                MessageBox.Show("权限不足！");
                return;
            }
            OpenWindow(typeof(frmMerchandiseOrderManger).ToString());
        }

        private void 查看库存ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (!CheckPermission(oShopConfigInfo.Permission, PermissionEnum.BaseManager))
            {
                MessageBox.Show("权限不足！");
                return;
            }
            OpenWindow(typeof(frmStockExamine).ToString());
        }

        private void 商品上架管理ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (!CheckPermission(oShopConfigInfo.Permission, PermissionEnum.MerchandiseManager))
            {
                MessageBox.Show("权限不足！");
                return;
            }
            OpenWindow(typeof(frmMerchandiseToSell).ToString());
        }

        private void 注销ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Hide();
            frmUserLogin loginForm = new frmUserLogin();
            loginForm.IsLogoff = true;
            loginForm.Show();
        }

        private void 退出系统ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void 今日销售ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenWindow(typeof(frmMerchandiseTodaySelled).ToString());
        }

        private void 商品报警管理ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (!CheckPermission(oShopConfigInfo.Permission, PermissionEnum.MerchandiseManager))
            {
                MessageBox.Show("权限不足！");
                return;
            }
            OpenWindow(typeof(frmMerchandiseStockLowAlertCheck).ToString());
        }

        private void 商品逾期报警ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (!CheckPermission(oShopConfigInfo.Permission, PermissionEnum.MerchandiseManager))
            {
                MessageBox.Show("权限不足！");
                return;
            }
            OpenWindow(typeof(frmMerchandiseOverTimeCheck).ToString());
        }

        private void 销售ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (!CheckPermission(oShopConfigInfo.Permission, PermissionEnum.MerchandiseManager))
            {
                MessageBox.Show("权限不足！");
                return;
            }
            OpenWindow(typeof(frmShopDailyReport).ToString());
        }

        private void 每日趋势ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (!CheckPermission(oShopConfigInfo.Permission, PermissionEnum.MerchandiseManager))
            {
                MessageBox.Show("权限不足！");
                return;
            }
            OpenWindow(typeof(frmShopTrendChartByDay).ToString());
        }

        private void 每月趋势ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (!CheckPermission(oShopConfigInfo.Permission, PermissionEnum.MerchandiseManager))
            {
                MessageBox.Show("权限不足！");
                return;
            }
            OpenWindow(typeof(frmShopTrendChartByMonth).ToString());
        }

        private void 热门商品ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (!CheckPermission(oShopConfigInfo.Permission, PermissionEnum.MerchandiseManager))
            {
                MessageBox.Show("权限不足！");
                return;
            }
            OpenWindow(typeof(frmShopHotMerchandise).ToString());
        }

        private void 会员管理ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (!CheckPermission(oShopConfigInfo.Permission, PermissionEnum.MemberManager))
            {
                MessageBox.Show("权限不足！");
                return;
            }
            OpenWindow(typeof(frmMemberManager).ToString());
        }

        private void 等级管理ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (!CheckPermission(oShopConfigInfo.Permission, PermissionEnum.MemberManager))
            {
                MessageBox.Show("权限不足！");
                return;
            }
            OpenWindow(typeof(frmMemberLevelManager).ToString());
        }

        private void 商品单位管理ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (!CheckPermission(oShopConfigInfo.Permission, PermissionEnum.MerchandiseManager))
            {
                MessageBox.Show("权限不足！");
                return;
            }
            OpenWindow(typeof(frmMerchandiseUnitManger).ToString());
        }

        private void 添加会员ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            OpenWindow(typeof(frmMemberAppend).ToString());
        }

        //
        /// <summary>
        /// Open child window
        /// </summary>
        /// <param name="ChildTypeString"></param>
        private void OpenWindow(string ChildTypeString)
        {
            Form myChild = null;
            if (!ContainMDIChild(ChildTypeString))
            {
                // Get current process assembly
                Assembly assembly = Assembly.GetExecutingAssembly();
                // Create data type using type string
                Type typForm = assembly.GetType(ChildTypeString);

                // Create object using type's "InvokeMember" method

                Object obj = typForm.InvokeMember(
                    null,
                    BindingFlags.DeclaredOnly |
                    BindingFlags.Public | BindingFlags.NonPublic |
                    BindingFlags.Instance | BindingFlags.CreateInstance,
                    null,
                    null,
                    null);
                // Show child form 
                if (obj != null)
                {
                    myChild = obj as Form;
                    myChild.MdiParent = this;
                    myChild.Show();
                    myChild.Focus();
                }
            }
        }

        /// <summary>
        /// Search mdi child form by specific type string
        /// </summary>
        /// <param name="ChildTypeString"></param>
        /// <returns></returns>
        private bool ContainMDIChild(string ChildTypeString)
        {
            Form myMDIChild = null;
            foreach (Form f in this.MdiChildren)
            {
                if (f.GetType().ToString() == ChildTypeString)
                {
                    // found it 
                    myMDIChild = f;
                    break;
                }
            }
            // Show the exist form

            if (myMDIChild != null)
            {
                myMDIChild.TopMost = true;
                myMDIChild.Show();
                myMDIChild.Focus();
                return true;
            }
            else return false;
        }


        //快捷图标
        private void 员工管理ToolStripButton_Click(object sender, EventArgs e)
        {
            会员管理ToolStripMenuItem_Click(sender, e);
        }

        private void btnCash_Click(object sender, EventArgs e)
        {
            开始收银ToolStripMenuItem_Click(sender, e);
        }

        private void btnDailyReport_Click(object sender, EventArgs e)
        {
            今日销售ToolStripMenuItem_Click(sender, e);
        }

        private void btnStock_Click(object sender, EventArgs e)
        {
            查看库存ToolStripMenuItem_Click(sender, e);
        }

        private void btnHot_Click(object sender, EventArgs e)
        {
            热门商品ToolStripMenuItem_Click(sender, e);
        }

        private void btnLock_Click(object sender, EventArgs e)
        {
            锁定窗口ToolStripMenuItem_Click(sender, e);
            //OpenWindow(typeof(frmRemindBrithday).ToString());
        }

        //窗口
        private void 层叠排列ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.Cascade);
        }

        private void 水平排列ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.TileHorizontal);
        }

        private void 垂直排列ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.TileVertical);
        }

        private void 全部关闭ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (Form childForm in MdiChildren)
            {
                childForm.Close();
            }
        }

        private void 重排图标ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.ArrangeIcons);
        }

        private void 锁定窗口ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            object o=new object();
            frmLockSetPassword oLockSetPassword = new frmLockSetPassword();
            oLockSetPassword.Owner = this;
            if (oLockSetPassword.ShowDialog() == DialogResult.OK)
            {
                //oLockSetPassword.Dispose();
                LockWindow();
            }
        }
        private void LockWindow()
        {
            frmLockInputPassword oLockInputPassword = new frmLockInputPassword();
            oLockInputPassword.Owner = this;
            this.Hide();
            if (oLockInputPassword.ShowDialog() == DialogResult.OK)
            {
                this.Show();
            }
            else
            {
                LockWindow();
            }
        }

        private void 利润统计ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (!CheckPermission(oShopConfigInfo.Permission, PermissionEnum.AdminManager))
            {
                MessageBox.Show("权限不足！");
                return;
            }
            OpenWindow(typeof(frmShopProfits).ToString());
        }

        private void 数据备份与恢复ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (!CheckPermission(oShopConfigInfo.Permission, PermissionEnum.AdminManager))
            {
                MessageBox.Show("权限不足！");
                return;
            }
            OpenWindow(typeof(frmDatabaseBackupOrRestore).ToString());
        }

        private void 生日提醒ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (!CheckPermission(oShopConfigInfo.Permission, PermissionEnum.AdminManager))
            {
                MessageBox.Show("权限不足！");
                return;
            }
            OpenWindow(typeof(frmRemindBrithday).ToString());
        }

        private void 系统设置ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenWindow(typeof(frmSystemConfig).ToString());
        }

        private void 商品捆绑ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (!CheckPermission(oShopConfigInfo.Permission, PermissionEnum.BaseManager))
            {
                MessageBox.Show("权限不足！");
                return;
            }
            OpenWindow(typeof(frmMerchandiseBindManager).ToString());
        }

        private void 关于系统ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenWindow(typeof(frmAboutProgram).ToString());
        }

        private void 历史版本ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenWindow(typeof(frmHistoryVersion).ToString());
        }

        private void btnSystemSet_Click(object sender, EventArgs e)
        {
            系统设置ToolStripMenuItem_Click(sender, e);
        }
    }
}
